package com.cs410.collabwriting.main;

import java.util.ArrayList;

import com.cs410.collabwriting.tools.DatabaseManager;
import com.cs410.collabwriting.tools.queryTools.DatabaseTable;
import com.cs410.collabwriting.tools.queryTools.QueryBuilder;
import com.cs410.collabwriting.tools.queryTools.QueryCondition;
import com.cs410.collabwriting.tools.queryTools.QueryType;
import com.cs410.collabwriting.tools.queryTools.tables.PROMPT_TABLE;

public class Prompt implements DatabaseObjects {
//JL: wtf i don't get this class.
//AC: how are we creating Prompts?!?!?
//AC: need to make sure to set ACTIVE to TRUE (aka 1) when creating a Prompt (by default in SQL it is FALSE (aka 0))
	private String m_textPrompt = null;
	private String m_imageFilePath = null;
	private boolean m_active = false;
	private int m_promptID = -1;
	private String m_userID = null;
	private String m_timeStamp = null;
	
	public Prompt() {
		
	}
	
	public Prompt( int promptID ) {
		ArrayList<String[]> results = DatabaseManager.executeQueryWithColumnNames(
			new QueryBuilder(QueryType.SELECTION)
			.addTables( DatabaseTable.PROMPT_TABLE )
			.addConditions( QueryCondition.AND, PROMPT_TABLE.PROMPTID, QueryCondition.EQ, promptID )
			.value() );
		
		if( results != null  && results.size() > 1 ) {
			m_textPrompt = DatabaseManager.getFirstValueInColumn( results, PROMPT_TABLE.PROMPT );
			m_imageFilePath = DatabaseManager.getFirstValueInColumn( results, PROMPT_TABLE.IMAGEPATH );
			m_userID = DatabaseManager.getFirstValueInColumn( results, PROMPT_TABLE.USERID );
			m_timeStamp = DatabaseManager.getFirstValueInColumn( results, PROMPT_TABLE.TIMESTAMP );
			String activeAsString = DatabaseManager.getFirstValueInColumn(results, PROMPT_TABLE.ACTIVE);
			
			if (activeAsString.compareTo("0") == 0) {
				m_active = false;
			} else {
				m_active = true;
			}
		}
		this.m_promptID = promptID;
	}
	
	public Prompt(String textPrompt) {
		this.m_textPrompt = textPrompt;
	}
	
	public Prompt(String textPrompt, String filePath) {
		this.m_textPrompt = textPrompt;
		this.m_imageFilePath = filePath;
	}
	
	public void setTextPrompt( String textPrompt ) {
		this.m_textPrompt = textPrompt;
	}
	
	public String getTextPrompt() {
		return m_textPrompt;
	}
	
	public void setImageFilePath( String filePath ) {
		this.m_imageFilePath = filePath;
	}
	
	public String getImageFilePath() {
		return m_imageFilePath;
	}
	
	public boolean isActive() {
		return m_active;
	}
	
	public int getID() {
		return m_promptID;
	}
	
	public String getUserID() {
		return m_userID;
	}
	
	public void setUserID( String userID ) {
		m_userID = userID;
	}
	
	public String getTimestamp() {
		return m_timeStamp;
	}
	
	public boolean deactivate() {
//		if( m_promptID >= 0 ) {
//			int updated = DatabaseManager.executeUpdate(
//				new QueryBuilder(QueryType.DELETE)
//				.addTables(DatabaseTable.PROMPT_TABLE)
//				.addConditions( QueryCondition.AND, PROMPT_TABLE.PROMPTID, QueryCondition.EQ, m_promptID )
//				.value() );
//			return updated > 0 ? true : false;
//		}
//		return false;
		if (m_active == true) {
			int updated = DatabaseManager.executeUpdate(
					new QueryBuilder(QueryType.UPDATE)
					.addColumns(PROMPT_TABLE.ACTIVE)
					.addTables(DatabaseTable.PROMPT_TABLE)
					.addValues("0") //0 indicates false
					.addConditions(QueryCondition.AND, PROMPT_TABLE.PROMPTID, QueryCondition.EQ, m_promptID)
					.value() );
			m_active = (updated > 0) ? false : true;
			return updated > 0 ? true : false;
		}
		return false;
	}
	
	public void update() {}
	
	public boolean create() {
		if( m_textPrompt != null || m_imageFilePath != null && m_userID != null) {
			QueryBuilder query = new QueryBuilder(QueryType.INSERT);
			query.addColumns(PROMPT_TABLE.USERID);
			query.addValues( m_userID );
			if( m_textPrompt != null ) {
				query.addColumns(PROMPT_TABLE.PROMPT);
				query.addValues( m_textPrompt );
			}
			if( m_imageFilePath != null ) {
				query.addColumns(PROMPT_TABLE.IMAGEPATH);
				query.addValues( m_imageFilePath );
			}
			query.addTables(DatabaseTable.PROMPT_TABLE);
			int success = DatabaseManager.executeUpdate( query );
			return success > 0 ? true : false;
		}
		return false;
	}

}
